<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Dyn extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $user_name = trim(input('user_name'), ' ');
            $banner_type = input('banner_type');
            $status = input('status');
            $where['h_type'] = 1;
            if(isset($user_name) && $user_name != ''){
                $where['user_username|h_title'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($banner_type)&&$banner_type!="")
            {
                $where['h_from'] = $banner_type;
            }
            if(isset($status)&&$status!="")
            {
                $where['h_status'] = $status;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_hot')
                ->join('up_u_user', 'user_id = h_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_hot')
                ->join('up_u_user', 'user_id = h_user_id', 'left')
                ->field('h_id, h_time, h_message, user_username,h_title,h_image,h_status,h_from,h_top,h_money,h_money_old')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('h_status asc,h_top desc,h_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    if($v['h_image']){
                        $lists[$k]['h_image'] = explode(',',$v['h_image']);
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 帖子详情
     * @return mixed
     */
    public function dynDetail(){
        $h_id = input('h_id');
        $detail = Db::table('up_hot h')
            ->join('up_u_user', 'user_id = h_user_id', 'left')
            ->field('h.*, user_username')
            ->where('h_id', $h_id)
            ->find();
        if($detail['h_image']){
            $img = trim($detail['h_image'], ',');
            $detail['h_image'] = explode(',', $img);
        }
        $from = [
            '3' => '教育',
            '4' => '校服',
            '5' => '设计',
            '6' => '闲置',
            '7' => '问答',
        ];
        $this->assign('detail', $detail);
        $this->assign('from', $from);
        return $this->fetch('dyn/dyndetail');
    }
    
    /**
     * 删除帖子
     * @return \think\response\Json
     */
    public function dynDel(){
        $h_id = input('id');
        $res = Db::table('up_hot')->delete($h_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 同意
     * @return \think\response\Json
     */
    public function agree(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 拒绝
     * @return \think\response\Json
     */
    public function refuse(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_status',3);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 置顶
     * @return \think\response\Json
     */
    public function dynTop(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_top', 2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 取消置顶
     * @return \think\response\Json
     */
    public function dynTopno(){
        $h_id = input('id');
        $res = Db::table('up_hot')->where('h_id',$h_id)->setField('h_top', 1);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 评论
     * @return mixed|\think\response\Json
     */
    public function dynComment(){
        if(request()->isAjax()){
            $where['comment_hot_id'] = session('h_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_hot_comment')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_hot_comment')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->field('comment_id, comment_content, comment_time, user_username')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('comment_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $h_id = input('h_id');
        session('h_id', $h_id);
        return $this->fetch('dyn/dyncomment');
    }
    
    /**
     * 删除评论
     * @return \think\response\Json
     */
    public function commentDel(){
        $comment_id = input('id');
        $dyn_id = Db::table('up_hot_comment')->where('comment_id', $comment_id)->value('comment_hot_id');
        Db::startTrans();
        try{
            // 删除评论
            Db::table('up_hot_comment')->delete($comment_id);
            // 帖子评论数减一
            Db::table('up_hot')->where('h_id', $dyn_id)->setDec('h_message');
            
            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 回复
     * @return mixed|\think\response\Json
     */
    public function commentReply(){
        if(request()->isAjax()){
            $where['comtt_comment_id'] = session('comment_id');
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_hot_comment_tt')
                ->join('up_u_user', 'user_id = comtt_reply_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_hot_comment_tt')
                ->join('up_u_user', 'user_id = comtt_reply_user_id', 'left')
                ->field('comtt_id, comtt_content, comtt_time, user_username')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('comtt_time desc')
                ->select();
            foreach($lists as $k => $v){
                if(strpos($v['comtt_content'], '//@:')){
                    $lists[$k]['comtt_content'] = strstr($v['comtt_content'], '//@:', true);
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $comment_id = input('comment_id');
        session('comment_id', $comment_id);
        return $this->fetch('dyn/commentreply');
    }
    
    /**
     * 删除回复
     * @return \think\response\Json
     */
    public function comttDel(){
        $comtt_id = input('id');
        $res = Db::table('up_hot_comment_tt')->delete($comtt_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}